﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using Willow.Authorization;
namespace Willow.WebApplication.Controllers
{
    public class CommonController : Controller
    {

        /// <summary>
        /// 生成Guid
        /// </summary>
        /// <returns></returns>
        public JsonResult GetGuid()
        {
            return Json(Guid.NewGuid().ToString(), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 生成工具栏HTML
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public ViewResult RenderToolbar()
        {
            var user = OnlineContext.Instance.GetCurrentUser();
            var url = this.Request.Url.AbsolutePath;
            var actions = user.LoginActions.Where(p => p.BelongURL == url && p.Category == ActionCategory.Button).OrderBy(p => p.DisplayOrder).ToList();
            return View(actions);
        }

        /// <summary>
        /// 生成选择图标对话框
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        [ChildActionOnly]
        public ViewResult RenderIconLookup(string id, string filePath)
        {
            string content = System.IO.File.ReadAllText(Server.MapPath(filePath));
            MatchCollection mt = Regex.Matches(content, @"\.(.*){", RegexOptions.Multiline);
            List<string> lstIcon = new List<string>();
            for (int i = 0; i < mt.Count; i++)
            {
                lstIcon.Add(mt[i].Groups[1].Value);
            }
            ViewBag.Icons = lstIcon;
            ViewBag.Id = id;
            return View();
        }
    }
}